package cn.chen.controller;

import cn.chen.model.User;
import cn.chen.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbpu
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //增
    @PostMapping("/add")
    public boolean addUser(@RequestBody  User user){
        return userService.save(user);
    }

    //删
    @GetMapping("/delete")
    public boolean deleteUser(User user){
        return userService.removeById(user);
    }

    //改
    @PostMapping("/update")
    public boolean updateUser(@RequestBody  User user){
        return userService.updateById(user);
    }

    //查
    @GetMapping("/get")
    public User getUser(Integer id){
        return userService.getById(id);
    }

    //登录
    @GetMapping("/login")
    public User login(User user){
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(User::getUsername,user.getUsername());
//        wrapper.eq(User::getPassword,user.getPassword());
//        //判断用户是否被禁用
//        wrapper.eq(User::getStatus,0);
//        return userService.getOne(wrapper);

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        //判断用户是否被禁用
        wrapper.eq("status",1);
        return userService.getOne(wrapper);
    }


}
